package com.lmm.lcbusiness.lcbusiness.pages.comweb;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import com.lmm.dresswisdom.lmmframe.components.alert.AlertUtils;
import com.lmm.dresswisdom.lmmframe.util.PackageUtils;
import com.lmm.dresswisdom.lmmframe.util.ToastUtils;
import com.lmm.lcbusiness.lcbusiness.AppBaseActivity;
import com.lmm.lcbusiness.lcbusiness.config.AppKey;
import com.lmm.lcbusiness.lcbusiness.config.AppKeyValueUtils;
import com.lmm.lcbusiness.lcbusiness.jsbridge.THJSWebViewBridge;
import com.lmm.lcbusiness.lcbusiness.jsbridge.THJSWebViewBridgeAction;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by xmgong on 2016/12/24.
 * 原生方法
 */

public class NaviMethodsUtils {


    private AppBaseActivity activity;
    private Context mcontext;
    private WebView webView;

    public String getCurPort() {
        return curPort;
    }

    private String curPort = "";

    public THJSWebViewBridge getThjsWebViewBridge() {
        return thjsWebViewBridge;
    }


    private THJSWebViewBridge thjsWebViewBridge;


    public NaviMethodsUtils(Context mcontext, AppBaseActivity appBaseActivity) {
        this.mcontext = mcontext;
        this.activity = appBaseActivity;
    }

    public void initWebBridge(WebView webView) {
        if (thjsWebViewBridge == null) {
            this.webView = webView;
            thjsWebViewBridge = new THJSWebViewBridge(webView);
        }
    }


    public void addComMethod() {
        //跳转
//        thjsWebViewBridge.register("navi", new THJSWebViewBridge.THJSWebViewBridgeCall() {
//            @Override
//            public void call(THJSWebViewBridgeAction action) {
//                try {
//                    String route = action.getData().getString("router");
//                    LogUtils.write(action.getMethod() + "//" + route);
//                    RouteManager.getInstance().doRouter(route, activity, 0, null);
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//        });


//        thjsWebViewBridge.register("setShoppingKey", new THJSWebViewBridge.THJSWebViewBridgeCall() {
//            @Override
//            public void call(THJSWebViewBridgeAction action) {
//
//            }
//        });

//        thjsWebViewBridge.register("showDefaultLoading", new THJSWebViewBridge.THJSWebViewBridgeCall() {
//            @Override
//            public void call(THJSWebViewBridgeAction action) {
//                if (activity == null) {
//                    LogUtils.write("先设置下setBaseWebActivity");
//                } else {
//                    JSONObject jsonObject = action.getData();
//                    String msg = "";
//                    try {
//                        msg = jsonObject.get("message").toString();
//                        activity.setLoadDialogTitle(msg);
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                        activity.setLoadDialogTitle("加载中...");
//                    }
//                    activity.showDefaultLoading();
//                }
//            }
//        });
//        thjsWebViewBridge.register("hideLoading", new THJSWebViewBridge.THJSWebViewBridgeCall() {
//            @Override
//            public void call(THJSWebViewBridgeAction action) {
//                if (activity == null) {
//                    LogUtils.write("先设置下setBaseWebActivity");
//                } else {
//                    activity.hideDefaultDialog();
//                }
//            }
//        });


        thjsWebViewBridge.register("goBack", new THJSWebViewBridge.THJSWebViewBridgeCall() {
            @Override
            public void call(THJSWebViewBridgeAction action) {
                if (activity != null) {
                    activity.finish();
                }
            }
        });

        thjsWebViewBridge.register("callPhone", new THJSWebViewBridge.THJSWebViewBridgeCall() {
            @Override
            public void call(THJSWebViewBridgeAction action) {
                JSONObject jsonObject = action.getData();
                try {
                    final String phone = jsonObject.getString("phone");
                    AlertUtils.showAlert(mcontext, "拨打号码", "是否拨打" + phone + "?", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //意图：想干什么事
                            Intent intent = new Intent();
                            intent.setAction(Intent.ACTION_CALL);
                            //url:统一资源定位符
                            //uri:统一资源标示符（更广）
                            intent.setData(Uri.parse("tel:" + phone));
                            //开启系统拨号器
                            activity.startActivity(intent);
                        }
                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        });

        thjsWebViewBridge.register("reload", new THJSWebViewBridge.THJSWebViewBridgeCall() {
            @Override
            public void call(THJSWebViewBridgeAction action) {
                webView.reload();
            }
        });

        setSPK();

        NaiBD();
    }


    public void setSPK() {
        thjsWebViewBridge.register("setShoppingKey", new THJSWebViewBridge.THJSWebViewBridgeCall() {
            @Override
            public void call(THJSWebViewBridgeAction action) {
                JSONObject jsonObject = action.getData();
                try {
                    String Spk = jsonObject.getString("shoppingKey");
                    AppKeyValueUtils.setValue(AppKey.ShopKey, Spk);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //导航
    public void NaiBD() {
        thjsWebViewBridge.register("gomap", new THJSWebViewBridge.THJSWebViewBridgeCall() {
            @Override
            public void call(THJSWebViewBridgeAction action) {
                JSONObject jsonObject = action.getData();
                try {
                    String lat = jsonObject.get("lat").toString();
                    String lng = jsonObject.get("lng").toString();

                    if (PackageUtils.isAvilible(mcontext, "com.baidu.BaiduMap")) {
                        Intent i1 = new Intent();
                        i1.setData(Uri.parse("baidumap://map/navi?location=" + lat + "," + lng));
                        mcontext.startActivity(i1);
                    } else {
                        ToastUtils.showToastShort("请先安装百度地图");
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }


//    public void addPay(final Activity activity, final AliPayUtils.PayReturn payReturn) {
//        thjsWebViewBridge.register("pay", new THJSWebViewBridge.THJSWebViewBridgeCall() {
//            @Override
//            public void call(THJSWebViewBridgeAction action) {
//                curPort = action.getPort();
//                try {
//                    JSONObject jsonObject = action.getData();
//                    String type = jsonObject.get("payType").toString();
//                    if (type.equals("1")) {
//                        String paystr = jsonObject.get("payString").toString();
//                        //支付宝
//                        AliPayUtils.Pay(paystr, activity, payReturn);
//                    } else if (type.equals("2")) {
//                        //微信
//                        WXPayUtils.Pay(ConfigValue.WXAPI, activity, action.getData().toString());
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//    }

//    ShareUtils shareUtils = null;
//    private String shareTitle = "";
//    private String shareUrl = "";
//    private String shareImgUrl = "";//分享图片
//    private String tips = "";//描述


//    public void addShare(final Activity activity, UMShareListener listener, final String title1) {
//        shareUtils = new ShareUtils(activity, listener);
//        shareTitle = title1;
//        thjsWebViewBridge.register("share", new THJSWebViewBridge.THJSWebViewBridgeCall() {
//            @Override
//            public void call(THJSWebViewBridgeAction action) {
//                curPort = action.getPort();
//
//                try {
//                    JSONObject jsonObject = action.getData();
//                    shareUrl = jsonObject.get("weburl").toString();
//                    if (jsonObject.has("title")) {
//                        shareTitle = jsonObject.get("title").toString();
//                    }
//
//                    if (jsonObject.has("shareimg")) {
//                        shareImgUrl = jsonObject.getString("shareimg");
//                    }
//
//                    if (jsonObject.has("tips")) {
//                        tips = jsonObject.getString("tips");
//                    }
//
//                } catch (Exception e) {
//                    shareUrl = ConfigValue.WEBURL_HOME;
//                }
//
//                activity.runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//
//                        if (shareImgUrl.equals("")) {
//                            shareUtils.ShareWithURL(shareUrl, shareTitle);
//                        } else {
//                            shareUtils.ShareWithURL(shareUrl, shareTitle, tips, shareImgUrl);
//                        }
//                    }
//                });
//            }
//        });
//    }

    //查看信息
//    public void addCheckMessage() {
//        thjsWebViewBridge.register("checkMessage", new THJSWebViewBridge.THJSWebViewBridgeCall() {
//            @Override
//            public void call(THJSWebViewBridgeAction action) {
//
//                JSONObject jsonObject = action.getData();
//
//                try {
//                    String infoid = jsonObject.getString("infoid");
//                    String shopId = jsonObject.getString("shopid");
//                    HashMap<String, Object> params = new HashMap<>();
//                    params.put("infoId", infoid);
//                    params.put("shopId", shopId);
//                    // 新闻详情
//                    RouteManager.getInstance().doRouter(RouteKeys.NewsDetail, activity, 0, params);
//                } catch (JSONException e) {
//                    e.printStackTrace();
//
//                }
//
//            }
//        });
//    }
}
